<?php
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\AuthController;
use Think\Auth;

class SysController extends AuthController {
    //站点设置
    public function sys(){
    	$sys=M('sys')->where(array('sys_id'=>1))->find();
    	$this->assign('sys',$sys)->display();
    }
    
    //保存站点设置
    public function runsys(){
    	if (!IS_AJAX){
    		$this->error('提交方式不正确',0,0);
    	}else{
    		M('sys')->save($_POST);
    		$this->success('站点设置保存成功',1,1);
    	}
    }

    //左侧栏目设置
    public function leftnav(){
    	$nav = new \Org\Util\Leftnav;
    	$adminnav=M('adminnav');
    	$navlist=$adminnav->order('adminnav_order')->select();
    	$arr = $nav::cznav($navlist);
    	$this->assign('arr',$arr);
    	$this->display();
    }
    
    public function leftnavadd(){
    	$adminnav_leftid=I('adminnav_leftid',0);
    	$adminnav=M('adminnav')->where(array('adminnav_id'=>$adminnav_leftid))->find();
    	$this->assign('adminnav',$adminnav);
    	$this->assign('adminnav_leftid',$adminnav_leftid);
    	$this->display();
    }
    
    //左侧栏目添加操作
    public function runleftnavadd(){
    	if (!IS_AJAX){
    		$this->error('提交方式不正确',0,0);
    	}else{
    		$adminnav=M('adminnav');
    		$renav=$adminnav->where(array('adminnav_title'=>I('adminnav_title')))->find();
    		if ($renav){
    			$this->error('导航标题不能重新，请重新输入',0,0);
    		}else{
    			$adminnav->add($_POST);
    			$this->success('左侧导航保存成功',1,1);
    		}
    	}
    }
    
    public function leftnavedit(){
    	$adminnav=M('adminnav')->where(array('adminnav_id'=>I('adminnav_id')))->find();
    	if ($adminnav['adminnav_leftid']!=0){
    		$adminnav_leftname=M('adminnav')->where(array('adminnav_id'=>$adminnav['adminnav_leftid']))->find();
    	}
    	$this->assign('adminnav_leftname',$adminnav_leftname);//上级导航名称
    	$this->assign('adminnav',$adminnav);//当前导航数据集
    	$this->display();
    }
    
    //左侧栏目添加操作
    public function runleftnavedit(){
    	if (!IS_AJAX){
    		$this->error('提交方式不正确',0,0);
    	}else{
    		$adminnav=M('adminnav');
    		$data=array(
    				'adminnav_id'=>I('adminnav_id'),
    				'adminnav_title'=>I('adminnav_title'),
    				'adminnav_url'=>I('adminnav_url'),
    				'adminnav_css'=>I('adminnav_css'),
    				'adminnav_open'=>I('adminnav_open',0),
    				'adminnav_order'=>I('adminnav_order'),
    		);
    		$adminnav->save($data);
    		$this->success('信息修改成功',1,1);

    	}
    }
    
    public function leftnavdel(){
    	$adminnav_id=I('adminnav_id');
    	$adminnav=M('adminnav')->where(array('adminnav_id'=>$adminnav_id))->delete();
    	$this->redirect('leftnav');
    }
    
    public function leftnavalldel(){
    	$left_id=I('left_id');
		if (!IS_AJAX){
    		$this->error('提交方式不正确',0,0);
    	}else{
    		if(is_array($left_id)){
    			$where = 'adminnav_id in('.implode(',',$left_id).')';
    		}else{
    			$where = 'adminnav_id='.$left_id;
    		}
    		$adminnav=M('adminnav')->where($where)->delete();
    		$this->success('批量删除成功',1,1);
    	}
    }
    
    public function leftnavorder(){
    	if (!IS_AJAX){
    		$this->error('排序提交方式不正确',0,0);
    	}else{
    		$adminnav=M('adminnav');
    		foreach ($_POST as $adminnav_id => $adminnav_order){
    			$adminnav->where(array('adminnav_id' => $adminnav_id ))->setField('adminnav_order' , $adminnav_order);
    		}
    		$this->success('排序更新成功',1,1);
    	}
    }
    
    //发送邮件设置
    public function emailsys(){
    	$sys=M('sys')->where(array('sys_id'=>1))->find();
    	$this->assign('sys',$sys)->display();
    }

    //保存站点设置
    public function runemail(){
    	if (!IS_AJAX){
    		$this->error('提交方式不正确',0,0);
    	}else{
    		M('sys')->save($_POST);
    		$this->success('邮件设置保存成功',1,1);
    	}
    }
    
    
}